import json
import re
from py_mini_racer import MiniRacer

class Utils(object):
    def __init__(self):
        self.resultJson = {}
        self.ctx = MiniRacer()
    
    def getToken(self, str):
        # 从文本获取token的值jQuery172011993289611962399_1737540926402({"token":"ld2NxPnx36F7IO0RnkXwF/gmvWUHB0DDP31hhq0qZ3HgN8mhAjnIgxHhMDaVKk1e"})
        # 使用正则表达式匹配 token 的值
        pattern = r'"token":"([^"]+)"'
        return re.search(pattern, str).group(1)
    
    def generateUrl(self, keyWords, pages, token_value):
        # 生成对应的地址
        # 初始化 MiniRacer 对象
        
        # 读取并执行 JavaScript 文件
        with open(r'waibao\news_spyder\guangming\guangming.js', 'r', encoding='utf-8') as file:
            js_code = file.read()
            self.ctx .eval(js_code)

        # 调用 JavaScript 函数，并传递参数
        return self.ctx.call('pcsearch', keyWords, pages, token_value)

    def strToJson(self, str):
        pattern = r'\?\((.*?)\)'
        matches = re.search(pattern, str)
        self.resultJson = json.loads(matches.group(1))

    
    def getPages(self):
        return self.resultJson["page"]["totalCount"] / self.resultJson["page"]["numPerPage"]

    def getAllUrls(self):
        urls = []
        lists = self.resultJson["result"]["list"]
        for i in lists:
            urls.append(i["url"])
            for j in i["itemList"]:
                urls.append(j["url"])
        return urls
    
if __name__ == '__main__':
    jsonStr = '?({"isBlock":false,"result":{"statistics":"3012万","total":"109259","count":"1200","cost":"115","list":[{"title":"做优增量盘活存量 全面提高资源配置效率","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_1-06.htm","synopsis":"通过完善资本市场功能，打通中长期资金、耐心资本入市的堵点卡点，强化金融资源对\u003cb\u003e科技\u003c/b\u003e型企业和战略性新兴产业的支持 ...","coverPic":"","master":"光明日报","pubtime":"2025-01-17 00:00:00","itemList":[{"title":"守牢全面推进乡村振兴的底线任务","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_2-06.htm","pubTime":"2025-01-17","version":"06"}],"count":"1200","cost":"115"},{"title":"广西崇左：探索以党建促产业深度融合","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_5-04.htm","synopsis":"组织聚链蓄势赋能产业向新发展提质 崇左科创甘蔗整秆收割机便是成果之一。为解决崇左甘蔗机收方面的相关难题，广西科创农业\u003cb\u003e科技\u003c/b\u003e集团董事长张胜忠带领团队深入田间地头，采集样本、分析数据，攻破刀盘自动升降等核心技术难题，研发的整秆式甘蔗收获机获得2024年广西国内首台（套）重大技术装备认定。携手并进共富同行（本报记者周仕兴本报通讯员韦龙海）","coverPic":"","master":"光明日报","pubtime":"2025-01-17 00:00:00","itemList":[{"title":"四川自贡国际恐龙灯会亮灯","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_8-04.htm","pubTime":"2025-01-17","version":"04"},{"title":"国家民委：健全铸牢中华民族共同体意识制度机制","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_2-04.htm","pubTime":"2025-01-17","version":"04"},{"title":"上海产业经济向“新”发力","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_4-04.htm","pubTime":"2025-01-17","version":"04"}],"count":"1200","cost":"115"},{"title":"共建友好家园 书写中国-东盟合作新篇章","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_1-12.htm","synopsis":"广西壮族自治区党委常委、南宁市委书记农生文在致辞中表示，作为中国-东盟博览会永久举办地，近年来，南宁紧紧围绕国家赋予的建设面向东盟开放合作的国际化大都市新定位，搭建起中国-东盟交流交往的重要桥梁纽带，实现了经贸、\u003cb\u003e科技\u003c/b\u003e、人文等领域的双向奔赴 ...","coverPic":"","master":"光明日报","pubtime":"2025-01-17 00:00:00","itemList":[{"title":"巴以停火协议，最终能实现吗？","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_2-12.htm","pubTime":"2025-01-17","version":"12"},{"title":"一路欢歌一路情","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_4-12.htm","pubTime":"2025-01-17","version":"12"},{"title":"“古蜀遗珍”三星堆出土文物图片展在土耳其安纳托利亚文明博物馆开幕","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_3-12.htm","pubTime":"2025-01-17","version":"12"}],"count":"1200","cost":"115"},{"title":"做好保障，增强归属感","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_5-03.htm","synopsis":"快递小哥章晓波就是这个服务平台的受益者。“工作群里每个月都会发布平台举办的各类技能培训班的信息，我之前在老家学过简单的电路维修，就报名参加了一期培训班，顺利拿到了低压电工证。”如今已成为一家\u003cb\u003e科技\u003c/b\u003e公司技术工人的章晓波说，拿到技能证后，街道还对接企业帮他找工作。（本报记者刘艳杰本报通讯员楚新平）","coverPic":"","master":"光明日报","pubtime":"2025-01-17 00:00:00","itemList":[{"title":"李强会见斯里兰卡总统迪萨纳亚克","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_1-03.htm","pubTime":"2025-01-17","version":"03"},{"title":"图片报道","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_8-03.htm","pubTime":"2025-01-17","version":"03"},{"title":"2025年美丽中国建设将聚焦八方面重点任务","url":"http://epaper.gmw.cn/gmrb/html/2025-01/17/nw.D110000gmrb_20250117_4-03.htm","pubTime":"2025-01-17","version":"03"}],"count":"1200","cost":"115"},{"title":"歌剧：艺术皇冠上的一颗明珠","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-13.htm","synopsis":"进入20世纪以来，人类经历了两次世界大战，社会生活、经济、\u003cb\u003e科技\u003c/b\u003e、人文思想等方面发展迅猛，引发社会心理的种种变化，各个领域都在求新求变 ...","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[],"count":"1200","cost":"115"},{"title":"国际教育\u003cb\u003e科技\u003c/b\u003e人才联动发展态势","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_2-14.htm","synopsis":"从历史经验看，教育、\u003cb\u003e科技\u003c/b\u003e、人才在世界主要国家实现现代化的过程中发挥了重要的战略性、支撑性作用，三者的有机融合更为各国打造世界主要科学中心、世界重要人才中心提供了强大引擎 ...","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[{"title":"全球数字经济竞争下的人才培养","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-14.htm","pubTime":"2025-01-16","version":"14"}],"count":"1200","cost":"115"},{"title":"经济衰退反思中的分歧与妥协","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-12.htm","synopsis":"在社会政策上，联盟党许诺将降低社会保障费至毛工资的40%，用基本保障金代替现有的“公民金”。在能源政策上，联盟党主张通过发展各种新型能源（包括核能）以及降低电税和电网费来保障电力供给和降低能源价格，更多采用碳排放交易和碳税的方法实现减排，放弃欧盟此前制定的2035年禁止燃油车进入市场的承诺。在产业政策上，其主张促进数字化和高\u003cb\u003e科技\u003c/b\u003e发展，提高国家和企业的研发比例，激励企业创新。","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[{"title":"聚焦2025年中国宏观经济形势","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_4-12.htm","pubTime":"2025-01-16","version":"12"},{"title":"丹麦企业看好中国市场","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_3-12.htm","pubTime":"2025-01-16","version":"12"},{"title":"尹锡悦被捕之后","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_2-12.htm","pubTime":"2025-01-16","version":"12"}],"count":"1200","cost":"115"},{"title":"全国青少年\u003cb\u003e科技\u003c/b\u003e创新大赛有了新变化","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-08.htm","synopsis":"全国青少年\u003cb\u003e科技\u003c/b\u003e创新大赛（以下简称“大赛”）1982年创办，是中国科学技术协会、国家自然科学基金委、共青团中央、全国妇联等共同主办的青少年\u003cb\u003e科技\u003c/b\u003e创新赛事，旨在发现和培养青少年\u003cb\u003e科技\u003c/b\u003e创新后备人才 ...","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[{"title":"中共江苏省委党校：强化党建引领 助力乡村振兴","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_7-08.htm","pubTime":"2025-01-16","version":"08"},{"title":"篮球陪伴“天使宝宝”们成长","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_5-08.htm","pubTime":"2025-01-16","version":"08"},{"title":"黑脸琵鹭“组团”觅食","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_6-08.htm","pubTime":"2025-01-16","version":"08"}],"count":"1200","cost":"115"},{"title":"加快构建区域医疗资源共享新格局","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-07.htm","synopsis":"2016年5月，习近平总书记在全国\u003cb\u003e科技\u003c/b\u003e创新大会、两院院士大会、中国科协第九次全国代表大会上提出，“发展低成本疾病防控和远程医疗技术，实现优质医疗卫生资源普惠共享” ...","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[{"title":"如何提升县域医疗服务能力","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_2-07.htm","pubTime":"2025-01-16","version":"07"}],"count":"1200","cost":"115"},{"title":"“看清”原子世界，有多难","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_1-16.htm","synopsis":"扫描探针显微镜是纳米\u003cb\u003e科技\u003c/b\u003e领域最伟大的发明之一，主要包括扫描隧道显微镜和原子力显微镜，它利用尖锐的针尖逐点扫描样品，可在原子、分子、纳米多尺度上获取表面的形貌和丰富的物性，已广泛应用于物理学、化学、材料科学和生物学等多个领域，改变了人类对物质的研究方式和基础认知。“看”更细微的世界揭示物质和生命科学奥秘","coverPic":"","master":"光明日报","pubtime":"2025-01-16 00:00:00","itemList":[{"title":"冰表面在－153℃时就开始融化","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_3-16.htm","pubTime":"2025-01-16","version":"16"},{"title":"善用人工智能护航“数字出海”","url":"http://epaper.gmw.cn/gmrb/html/2025-01/16/nw.D110000gmrb_20250116_2-16.htm","pubTime":"2025-01-16","version":"16"}],"count":"1200","cost":"115"}]},"keywords":"科技","searchItem":{"timeOrder":true,"adv":false,"title":false,"fuzzyMatch":false},"page":{"totalCount":1200,"numPerPage":10,"pageNumShown":10,"currentPage":10,"pageCount":0},"dateType":"default"})'
    u = Utils(jsonStr=jsonStr)
    res = u.getAllUrls()
    print(res)
    print(u.getPages())